var tools = require('../utils/tools.js');
var moment = require('moment');
module.exports = function() {
    return function(model) {
        var model = model;
        this.save = function(camp, cb) {
            camp.createTime = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
            if(camp.openid){
                camp._id = camp.openid;
            }else{
                camp._id = tools.guid();
            }
            var entity = new model(camp);
            entity.save(function(err) {
                tools.execCB(cb, err, null);
            });
        };
        this.remove = function(conditions, cb) {
            model.remove(conditions, function() {
                tools.execCB(cb, err, null);
            });
        };
        this.removeById = function(id, cb) {
            model.remove({ _id: id }, function() {
                tools.execCB(cb, err, null);
            });
        };
        this.update = function(conditions, objNew, option, cb) {
            var option = option || {};
            model.update(conditions, objNew, option, function(err) {
                tools.execCB(cb, err, null);
            });
        };
        this.find = function(conditions, cb) {
            model.find(conditions, function(err,result) {
                tools.execCB(cb, err, result);
            });
        };
        this.findById = function(id, cb) {
            model.find({ _id: id }, function(err,result) {
                tools.execCB(cb, err, result);
            });
        };
        this.findByOpenId = function(id, cb) {
            model.find({ openid: id }, function(err,result) {
                tools.execCB(cb, err, result);
            });
        };

        return this;
    }
}
